home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / DynArray.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  3KB  |  138 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_General
  4. #include "General.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Memory
  8. #include "Memory.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_IO
  12. #include "IO.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_DynArray
  16. #include "DynArray.h"
  17. #endif
  18.  
  19.  
  20. static LONGINT AlignedSize ARGS((LONGINT ElmtSize));
  21.  
  22.  
  23. void DynArray_MakeArray
  24. # ifdef __STDC__
  25. (ADDRESS *ArrayPtr, LONGINT *ElmtCount, LONGINT ElmtSize)
  26. # else
  27. (ArrayPtr, ElmtCount, ElmtSize)
  28. ADDRESS *ArrayPtr;
  29. LONGINT *ElmtCount;
  30. LONGINT ElmtSize;
  31. # endif
  32. {
  33.   ElmtSize = AlignedSize(ElmtSize);
  34.   switch (ElmtSize % 4) {
  35.   case 0:;
  36.     break;
  37.   case 2:;
  38.     if (ODD(*ElmtCount)) {
  39.       INC(*ElmtCount);
  40.     }
  41.     break;
  42.   case 1:;
  43.   case 3:;
  44.     INC1(*ElmtCount, sizeof(LONGINT) - 1 - (*ElmtCount - 1) % sizeof(LONGINT));
  45.     break;
  46.   }
  47.   *ArrayPtr = Memory_Alloc(*ElmtCount * ElmtSize);
  48.   if (*ArrayPtr == NIL) {
  49.     IO_WriteS((System_tFile)IO_StdError, (STRING)"MakeArray: out of memory", 24L);
  50.     IO_WriteNl((System_tFile)IO_StdError);
  51.   }
  52. }
  53.  
  54. void DynArray_ExtendArray
  55. # ifdef __STDC__
  56. (ADDRESS *ArrayPtr, LONGINT *ElmtCount, LONGINT ElmtSize)
  57. # else
  58. (ArrayPtr, ElmtCount, ElmtSize)
  59. ADDRESS *ArrayPtr;
  60. LONGINT *ElmtCount;
  61. LONGINT ElmtSize;
  62. # endif
  63. {
  64.   ADDRESS NewPtr;
  65.   LONGINT *Source, *Target;
  66.   LONGINT i;
  67.  
  68.   ElmtSize = AlignedSize(ElmtSize);
  69.   NewPtr = Memory_Alloc(*ElmtCount * ElmtSize * 2);
  70.   if (NewPtr == NIL) {
  71.     IO_WriteS((System_tFile)IO_StdError, (STRING)"ExtendArray: out of memory", 26L);
  72.     IO_WriteNl((System_tFile)IO_StdError);
  73.   } else {
  74.     Source = (LONGINT *)(*ArrayPtr);
  75.     Target = (LONGINT *)NewPtr;
  76.     {
  77.       LONGINT B_1 = 1, B_2 = *ElmtCount * ElmtSize / sizeof(LONGINT);
  78.  
  79.       if (B_1 <= B_2)
  80.         for (i = B_1;; i += 1) {
  81.           *Target = *Source;
  82.           Source = (LONGINT *)(ADDRESS)((ADDRESS)Source + sizeof(LONGINT));
  83.           Target = (LONGINT *)(ADDRESS)((ADDRESS)Target + sizeof(LONGINT));
  84.           if (i >= B_2) break;
  85.         }
  86.     }
  87.     Memory_Free(*ElmtCount * ElmtSize, *ArrayPtr);
  88.     INC1(*ElmtCount, *ElmtCount);
  89.   }
  90.   *ArrayPtr = NewPtr;
  91. }
  92.  
  93. void DynArray_ReleaseArray
  94. # ifdef __STDC__
  95. (ADDRESS *ArrayPtr, LONGINT *ElmtCount, LONGINT ElmtSize)
  96. # else
  97. (ArrayPtr, ElmtCount, ElmtSize)
  98. ADDRESS *ArrayPtr;
  99. LONGINT *ElmtCount;
  100. LONGINT ElmtSize;
  101. # endif
  102. {
  103.   ElmtSize = AlignedSize(ElmtSize);
  104.   Memory_Free(*ElmtCount * ElmtSize, *ArrayPtr);
  105. }
  106.  
  107. static LONGINT AlignedSize
  108. # ifdef __STDC__
  109. (LONGINT ElmtSize)
  110. # else
  111. (ElmtSize)
  112. LONGINT ElmtSize;
  113. # endif
  114. {
  115.   LONGINT Align;
  116.  
  117.   if (ElmtSize >= General_MaxAlign) {
  118.     Align = General_MaxAlign;
  119.   } else {
  120.     Align = General_Exp2(General_Log2(ElmtSize + ElmtSize - 2));
  121.   }
  122.   return ElmtSize + Align - 1 - (ElmtSize - 1) % Align;
  123. }
  124.  
  125. void BEGIN_DynArray()
  126. {
  127.   static BOOLEAN has_been_called = FALSE;
  128.  
  129.   if (!has_been_called) {
  130.     has_been_called = TRUE;
  131.  
  132.     BEGIN_General();
  133.     BEGIN_Memory();
  134.     BEGIN_IO();
  135.  
  136.   }
  137. }
  138.